﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
//using ArchPostprocessing.CompoundStructureManage.Utility;
using YArchitech.LIB;

namespace HYOpeningDetail
{
	public partial class OpeningViewFloorSelectForm : System.Windows.Forms.Form
	{
		public CheckedListBox ChklSelViews
		{
			get
			{
				return this.chklSelViews;
			}
		}

		public OpeningViewFloorSelectForm()
		{
			this.InitializeComponent();
		}

		private void btnSet_Click(object sender, EventArgs e)
		{
			if (new OpeningViewDataSetForm().ShowDialog() == DialogResult.OK)
			{
				this.WriteSetDataToFile("CmdOpeningDetail", "CmdOpeningDetailData");
			}
		}

		private void btnAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chklSelViews.Items.Count; i++)
			{
				this.chklSelViews.SetItemChecked(i, true);
			}
		}

		private void btnNoSel_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chklSelViews.Items.Count; i++)
			{
				this.chklSelViews.SetItemChecked(i, false);
			}
		}

		private void btnInvert_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chklSelViews.Items.Count; i++)
			{
				if (this.chklSelViews.GetItemCheckState(i) == CheckState.Checked)
				{
					this.chklSelViews.SetItemChecked(i, false);
				}
				else
				{
					this.chklSelViews.SetItemChecked(i, true);
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		public List<int> SelectedViewIndexs()
		{
			List<int> list = new List<int>();
			for (int i = 0; i < this.chklSelViews.Items.Count; i++)
			{
				if (this.chklSelViews.GetItemChecked(i))
				{
					list.Add(i);
				}
			}
			return list;
		}

		private void OpeningViewFloorSelectForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
		}

		public bool ReadSetDataForm(string strCommandName, string strRevitFile, ref bool IsFlag)
		{
			string value = strRevitFile.Split(new char[]
			{
				'\\'
			}).Last<string>().ToString();
			string dataLocation = Product.DataLocation;
			string strUseFrontPath = this.m_strUseFrontPath;
			string text = Path.Combine(dataLocation, strUseFrontPath, this.m_strBaseXmlFileName);
			string sourceFileName = Path.Combine(dataLocation, this.m_strBaseXmlFileName);
			try
			{
				if (!File.Exists(text))
				{
					File.Copy(sourceFileName, text);
					IsFlag = false;
				}
				else
				{
					IsFlag = true;
				}
                //ArchPostprocessing.CompoundStructureManage.Utility.XmlHelper2 xmlHelper = new ArchPostprocessing.CompoundStructureManage.Utility.XmlHelper2(text, this.m_strNodeNameOfRoot);
                //XmlElement xmlRootNode = xmlHelper.XmlRootNode;
                //List<XmlElement> elements = xmlHelper.GetElements();
                //for (int i = 0; i < elements.Count; i++)
                //{
                //    string attribute = elements[i].GetAttribute(this.m_strNodeNameOfCmd);
                //    string attribute2 = elements[i].GetAttribute(this.m_strNodeNameOfRevitPath);
                //    if (attribute.Equals(strCommandName) && attribute2.Equals(value))
                //    {
                //        new List<XmlElement>();
                //        IEnumerator enumerator = elements[i].ChildNodes.GetEnumerator();
                //        {
                //            if (enumerator.MoveNext())
                //            {
                //                XmlElement xmlElement = enumerator.Current as XmlElement;
                //                xmlElement.GetAttribute("BChkBeamDimTop");
                //                xmlElement.GetAttribute("BChkBeamDimBot");
                //                xmlElement.GetAttribute("BChkBeamDimGrid");
                //                xmlElement.GetAttribute("BChkBeamDimWall");
                //                return true;
                //            }
                //        }
                //    }
                //}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		public bool WriteSetDataToFile(string strCommandName, string strRevitFile)
		{
			string text = strRevitFile.Split(new char[]
			{
				'\\'
			}).Last<string>().ToString();
			string dataLocation = Product.DataLocation;
			string strUseFrontPath = this.m_strUseFrontPath;
			string text2 = Path.Combine(dataLocation, strUseFrontPath, this.m_strBaseXmlFileName);
			string sourceFileName = Path.Combine(dataLocation, this.m_strBaseXmlFileName);
			try
			{
				if (!File.Exists(text2))
				{
					File.Copy(sourceFileName, text2);
				}
                //ArchPostprocessing.CompoundStructureManage.Utility.XmlHelper2 xmlHelper = new ArchPostprocessing.CompoundStructureManage.Utility.XmlHelper2(text2, this.m_strNodeNameOfRoot);
                //XmlElement xmlRootNode = xmlHelper.XmlRootNode;
                //XmlElement xmlCurNode = xmlHelper.XmlCurNode;
                //XmlDocument xmlDoc = xmlHelper.XmlDoc;
                //foreach (XmlElement xmlElement in xmlHelper.GetElements())
                //{
                //    string attribute = xmlElement.GetAttribute(this.m_strNodeNameOfCmd);
                //    string attribute2 = xmlElement.GetAttribute(this.m_strNodeNameOfRevitPath);
                //    if (strCommandName.Equals(attribute) && text.Equals(attribute2))
                //    {
                //        xmlRootNode.RemoveChild(xmlElement);
                //    }
                //}
                //XmlElement xmlElement2 = xmlDoc.CreateElement(this.m_strNameRviteNode);
                //this.AddNodeAttibute(xmlDoc, xmlRootNode, xmlElement2, this.m_strNodeNameOfCmd, strCommandName);
                //this.AddNodeAttibute(xmlDoc, xmlRootNode, xmlElement2, this.m_strNodeNameOfRevitPath, text);
                //XmlElement xmlElement3 = xmlDoc.CreateElement("BChkBeamDimTop");
                //XmlDocument xmlDoc2 = xmlDoc;
                //XmlElement fatherNode = xmlElement2;
                //XmlElement curNode = xmlElement3;
                //string strNameAttribute = this.m_strNameAttribute;
                //bool flag = OpeningViewDataSetForm.BChkBeamDimTop;
                //this.AddNodeAttibute(xmlDoc2, fatherNode, curNode, strNameAttribute, flag.ToString());
                //xmlDoc.CreateElement("BChkBeamDimBot");
                //XmlDocument xmlDoc3 = xmlDoc;
                //XmlElement fatherNode2 = xmlElement2;
                //XmlElement curNode2 = xmlElement3;
                //string strNameAttribute2 = this.m_strNameAttribute;
                //flag = OpeningViewDataSetForm.BChkBeamDimBottom;
                //this.AddNodeAttibute(xmlDoc3, fatherNode2, curNode2, strNameAttribute2, flag.ToString());
                //xmlDoc.CreateElement("BChkBeamDimGrid");
                //XmlDocument xmlDoc4 = xmlDoc;
                //XmlElement fatherNode3 = xmlElement2;
                //XmlElement curNode3 = xmlElement3;
                //string strNameAttribute3 = this.m_strNameAttribute;
                //flag = OpeningViewDataSetForm.BRadSelGrid;
                //this.AddNodeAttibute(xmlDoc4, fatherNode3, curNode3, strNameAttribute3, flag.ToString());
                //xmlDoc.CreateElement("BChkBeamDimWall");
                //XmlDocument xmlDoc5 = xmlDoc;
                //XmlElement fatherNode4 = xmlElement2;
                //XmlElement curNode4 = xmlElement3;
                //string strNameAttribute4 = this.m_strNameAttribute;
                //flag = OpeningViewDataSetForm.BRadSelWall;
                //this.AddNodeAttibute(xmlDoc5, fatherNode4, curNode4, strNameAttribute4, flag.ToString());
                //xmlHelper.SaveXml(text2);
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		private void AddNodeAttibute(XmlDocument xmlDoc, XmlElement fatherNode, XmlElement curNode, string strAttributeName, string strAttribute)
		{
			XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(strAttributeName);
			xmlAttribute.InnerText = strAttribute;
			curNode.Attributes.Append(xmlAttribute);
			fatherNode.AppendChild(curNode);
		}

		private string m_strUseFrontPath = Product.UserDataLocation;

		private string m_strBaseXmlFileName = "DataSet.xml";

		private string m_strNodeNameOfRoot = "Root";

		private string m_strNodeNameOfCmd = "CommandName";

		private string m_strNodeNameOfRevitPath = "RvtPathName";

		private string m_strNameAttribute = "Name";

		private string m_strNameRviteNode = "RvtName";

		private string m_strNameDataNode = "DataNode";
	}
}
